import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
// 引入cookie
import { getCookie } from '@/utils/cookie.js'
// 引入角色接口
import { getPress } from '@/http/http'
// 引入store实例对象
import store from '@/store'

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};

let routes = [
    {
        path: '/',
        redirect: '/layout'
    },
    {
        path: "/layout",
        component: () => import('@/views/layout/layout'),
        name: 'layout',
        children: [
            {
                path: '',
                component: () => import('@/views/home/Home.vue'),
                name: 'Home',
                meta: {
                    title: '首页',
                    icon: 'el-icon-attract'
                }
            },
            {
                path: 'user',
                name: 'User',
                component: () => import('@/views/user/User.vue'),
                meta: {
                    title: '用户管理',
                    icon: 'el-icon-coordinate'
                }
            },
            {
                path: 'msg',   // 信息管理功能
                name: 'Msg',
                component: () => import('@/views/msg/Msg.vue'),
                meta: {
                    title: '信息管理功能',
                    icon: 'el-icon-wallet'
                },
                children: [
                    {
                        path: 'mymsg',   //个人信息
                        name: 'Mymsg',
                        component: () => import('@/views/msg/Mymsg.vue'),
                        meta: {
                            title: '个人信息',
                            icon: 'el-icon-set-up'
                        },
                    }
                ]
            }
        ]
    },
    {
        path: '/login',
        component: () => import('@/views/Login.vue'),
        name: 'login'
    }

]
const router = new VueRouter({
    mode: "history",
    routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
    // 获取token
    let token = getCookie('token')
    if (token) {
        // 用户已经登陆
        // next()
        // 获取角色role
        let role = localStorage.getItem('role')
        console.log(role)

        if (store.state.routes.length == 0) {
            // 请求路由角色接口
            getPress({ role: role }).then(res => {
                console.log(res)
                if (res.code == '200') {
                    res.data.forEach((item) => {
                        item.component = () => import('@/views/' + item.path + '/' + item.name)
                        routes[1].children.push(item)
                    })
                    console.log(routes)

                    // 将添加修改完的routes 赋值给Vuex全局变量
                    store.dispatch('setRoleAction', routes).then(() => {
                        router.addRoutes(routes)
                        next({ ...to, replace: true }) //解决动态添加路由白屏问题  bug
                    })
                }
            })
        } else {
            next()
        }

    } else if (!token && to.path != '/login') {
        // 用户未登录
        next('/login')
    } else {
        next()
    }
})


export default router